package net.sf.jgcs.adapters;

import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

import net.sf.jgcs.ControlListener;

public class ExtendedControlListener implements ControlListener{

	private List<ControlListener> listeners = null;
	
	public ExtendedControlListener(){
		listeners = new ArrayList<ControlListener>();
	}
	
	public void add(ControlListener listener){
		this.listeners.add(listener);
	}
	
	public void remove(ControlListener listener){
		this.listeners.remove(listener);
	}
	
	public void removeAll(){
		this.listeners.clear();
	}
	
	public List<ControlListener> getListeners(){
		//return a clone because the list should be only modified inside the class...
		return new ArrayList<ControlListener>(listeners);
	}
	
	public void onFailed(SocketAddress peer) {
		for (ControlListener listener : listeners) {
			listener.onFailed(peer);
		}
	}

	public void onJoin(SocketAddress peer) {
		for (ControlListener listener : listeners) {
			listener.onJoin(peer);
		}
	}

	public void onLeave(SocketAddress peer) {
		for (ControlListener listener : listeners) {
			listener.onLeave(peer);
		}
	}
}
